//
//  JKViewShopsSections.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKViewShopsSections.h"

@implementation JKViewShopsSections
{
    UIColor *_color;
}
- (instancetype)init {
    
    if (self = [super init]) {
        
        _color = rgba(118, 197, 250, 1);
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    
    NSArray *sections = @[@"美食", @"休闲娱乐", @"生活服务", @"旅行", @"其他"];
    
    UIControl *last = nil;
    for (int i = 0; i < sections.count; i ++) {
        
        UIControl *view = [UIControl new];
        [view addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self).multipliedBy(1.0 / sections.count);
            make.top.equalTo(@0);
            if (last) {
                make.left.equalTo(last.mas_right);
            } else {
                make.left.equalTo(@0);
            }
            if (i == 0) {
                make.bottom.equalTo(@0);
            }
        }];
        
        last = view;
        
      
        

        UIImageView *imageView = [UIImageView new];
        imageView.tag = 10;
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"商家-%@-未选中", sections[i]]];
        [view addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(view.mas_width).multipliedBy(0.7);
            make.centerX.equalTo(@0);
            make.top.equalTo(@10);
        }];
        
        UILabel *label = [[UILabel alloc] init];
        label.tag = 20;
        label.text = sections[i];
        label.font = FONT(14);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor lightGrayColor];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.top.equalTo(imageView.mas_bottom).offset(10);
            make.height.equalTo(@15);
            
            make.bottom.equalTo(@-10);
        }];
    }
    
    
    
    [self tapAction:self.subviews.firstObject];
}

- (void)tapAction:(UIControl *)view {
    
    if (view.selected) {
        return;
    }
    NSArray *sections = @[@"美食", @"休闲娱乐", @"生活服务", @"旅行", @"其他"];
    
    if ([self.subviews indexOfObject:view] == 4) {
        if (self.selectBlock) {
            self.selectBlock(4);
        }
        return;
    }
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIControl * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIImageView *imageView = [obj viewWithTag:10];
        UILabel *label = [obj viewWithTag:20];

        
        if (obj == view) {
            obj.selected = true;
            if (self.selectBlock) {
                self.selectBlock(idx);
            }
            
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"商家-%@-选中", sections[idx]]];
            label.textColor = _color;
            
        } else {
            obj.selected = false;
            
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"商家-%@-未选中", sections[idx]]];
            label.textColor = [UIColor lightGrayColor];

        }
    }];
}


@end
